﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YJKFamilyLibDataManager;

namespace YJKFamilyLibManager.FamilyLibModule
{
	public partial class DlgAddParam : System.Windows.Forms.Form
	{
		public DlgAddParam(Document doc, DbFamily family)
		{
			this.mDoc = doc;
			this.mFamily = family;
			this.InitializeComponent();
		}

		private void DlgAddParam_Load(object sender, EventArgs e)
		{
			this.cbxType.SelectedIndex = 0;
			BuiltInParameterGroupMap builtInParameterGroupMap = new BuiltInParameterGroupMap();
			foreach (KeyValuePair<string, BuiltInParameterGroup> keyValuePair in builtInParameterGroupMap.Map)
			{
				this.cbxGroup.Items.Add(keyValuePair);
			}
			if (this.cbxGroup.Items.Count > 0)
			{
				this.cbxGroup.SelectedIndex = 0;
			}
		}

		private void cbxType_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cbxType.SelectedIndex >= 0)
			{
				this.cbxParamType.Items.Clear();
				ParameterTypeMap parameterTypeMap = new ParameterTypeMap();
				Dictionary<string, ParameterType> dictionary = parameterTypeMap.Map[this.cbxType.SelectedItem.ToString().Trim()];
				foreach (KeyValuePair<string, ParameterType> keyValuePair in dictionary)
				{
					this.cbxParamType.Items.Add(keyValuePair);
				}
				if (this.cbxParamType.Items.Count > 0)
				{
					this.cbxParamType.SelectedIndex = 0;
				}
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.txtName.Text != "")
			{
				List<DbParamDefinition> list = this.mFamily.Database.FamilyParamTable[this.mFamily];
				ParamDefinitionType pDefinitionType;
				if (this.rbType.Checked)
				{
					pDefinitionType = ParamDefinitionType.CustomParam;
				}
				else
				{
					pDefinitionType = ParamDefinitionType.InstanceParam;
				}
				Family family = this.mFamily.Load(this.mDoc);
				Document document = this.mDoc.EditFamily(family);
				if (document == null || !document.IsFamilyDocument)
				{
					return;
				}
				Transaction transaction = new Transaction(document, "TestCreateParem");
				try
				{
					try
					{
						transaction.Start();
						FamilyManager familyManager = document.FamilyManager;
						DbParamDefinition dbParamDefinition = new DbParamDefinition(this.txtName.Text.ToString(), ((KeyValuePair<string, ParameterType>)this.cbxParamType.SelectedItem).Value, pDefinitionType, ((KeyValuePair<string, BuiltInParameterGroup>)this.cbxGroup.SelectedItem).Value);
						FamilyParameter familyParameter = familyManager.AddParameter(dbParamDefinition.Name, dbParamDefinition.BuiltInParameterGroup, dbParamDefinition.ParameterType, dbParamDefinition.IsInstance);
						dbParamDefinition.StorageType = familyParameter.StorageType;
                        dbParamDefinition.DisplayUnitType = (DisplayUnitType)(-2);
						try
						{
							dbParamDefinition.DisplayUnitType = familyParameter.DisplayUnitType;
						}
						catch
						{
						}
						transaction.Commit();
						if (!list.Contains(dbParamDefinition))
						{
							list.Add(dbParamDefinition);
							this.mFamily.UpdateTypeTableColumn(list);
							MessageBox.Show("添加成功。");
							base.Close();
						}
						else
						{
							MessageBox.Show("参数重名，请重新输入。");
							this.txtName.Text = "";
						}
					}
					catch
					{
						MessageBox.Show("添加异常，请检查参数是否重名。");
						transaction.RollBack();
					}
					return;
				}
				finally
				{
					FileInfo fileInfo = new FileInfo(Path.Combine(Path.GetTempPath(), "tempRfa.rfa"));
					RevitVersionFuncs.SaveAs(document, fileInfo.FullName);
					document.Close(false);
					this.mFamily.Database.FamilyFileTable.Replace(this.mFamily, fileInfo);
					fileInfo.Delete();
				}
			}
			MessageBox.Show("参数名不正确，请重新输入。");
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void cbxGroup_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private Document mDoc;

		private DbFamily mFamily;
	}
}
